﻿Imports SIRData
Public Class frmTomaDeInventarioListado

  Private m_bKey As Boolean = False

  Private Sub frmTomaDeInventarioListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
      Me.Icon = My.Resources.iconInventory

      Call ListarInventario(True)

      cbovEstado.SelectedIndex = 0

      m_bKey = True ' Establecer la llave

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Public Sub ListarInventario(ByVal bTodos As Boolean)
    Dim m_Inventario As New clsInventario(g_sConnectString)
    Try

      If bTodos Then
        dgvListado.DataSource = m_Inventario.InventarioListar()
      Else
        dgvListado.DataSource = m_Inventario.InventarioListar(dtpFechaIni.Value, dtpFechaFin.Value, Microsoft.VisualBasic.Left(cbovEstado.Text, 1))
      End If

    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      m_Inventario = Nothing
      GC.Collect()
    End Try
  End Sub

  Private Sub dgvListado_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles dgvListado.RowPrePaint
    Try
      dgvListado.Rows(e.RowIndex).DefaultCellStyle.BackColor = IIf(e.RowIndex Mod 2 = 0, Color.Honeydew, Color.White)
      dgvListado.Rows(e.RowIndex).DefaultCellStyle.Font = New Font("Lucida Sans", 8)
    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub dgvListado_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvListado.CellContentClick
    Try
      'Esta parte me permite seleccionar solo un checkbox y se elimina el anterior 
      dgvListado.Rows(e.RowIndex).Cells("chkSel").Value = Not dgvListado.Rows(e.RowIndex).Cells("chkSel").Value
      For Each fila As DataGridViewRow In dgvListado.Rows
        If Not fila Is dgvListado.Rows(dgvListado.CurrentCell.RowIndex) Then
          fila.Cells("chkSel").Value = False
        End If
      Next

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnFiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFiltrar.Click
    Try
      Call ListarInventario(False)

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
    Dim oForm As New frmTomaDeInvetarioNuevo
    Try
      oForm.bListado = True
      oForm.bNuevo = True
      oForm.ShowDialog()

      Call ListarInventario(True)

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      oForm = Nothing : GC.Collect()
    End Try
  End Sub

  Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
    Dim oForm As New frmTomaDeInvetarioNuevo
    Try
      If dgvListado.Item("chkSel", dgvListado.CurrentCell.RowIndex).Value = False Then
        MessageBox.Show("Debe seleccionar con un check el registro que desea editar.", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
      Else
        If dgvListado.Item("cEstado", dgvListado.CurrentCell.RowIndex).Value = "C" Then
          MessageBox.Show("No se puede editar el registro, ya que se encuentra con estado CERRADO", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else

          oForm.oiNumInv = dgvListado.Item("iNumInv", dgvListado.CurrentCell.RowIndex).Value
          oForm.ocNumInv = dgvListado.Item("cNumInv", dgvListado.CurrentCell.RowIndex).Value
          oForm.lblcNumInv.Text = dgvListado.Item("cNumInv", dgvListado.CurrentCell.RowIndex).Value
          oForm.lblvNomResp.Tag = dgvListado.Item("siCodResp", dgvListado.CurrentCell.RowIndex).Value
          oForm.lblvNomResp.Text = dgvListado.Item("vNomResp", dgvListado.CurrentCell.RowIndex).Value
          oForm.lblvNomAlm.Tag = dgvListado.Item("siCodAlm", dgvListado.CurrentCell.RowIndex).Value
          oForm.lblvNomAlm.Text = dgvListado.Item("vNomAlm", dgvListado.CurrentCell.RowIndex).Value
          oForm.dtpsdFecDoc.Value = dgvListado.Item("sdFecInv", dgvListado.CurrentCell.RowIndex).Value
          oForm.cbovEstado.SelectedIndex = IIf(dgvListado.Item("cEstado", dgvListado.CurrentCell.RowIndex).Value = "C", 1, 2)

          oForm.bNuevo = False
          oForm.ShowDialog()

          Call ListarInventario(True)
        End If
      End If

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Public Sub EliminarInventario(ByVal piNumInv As Int32)
    Dim m_Inventario As New clsInventario(g_sConnectString)
    Try
      m_Inventario.InventarioEliminar(piNumInv)

    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      m_Inventario = Nothing
      GC.Collect()
    End Try
  End Sub

  Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
    Try
      If dgvListado.Item("chkSel", dgvListado.CurrentCell.RowIndex).Value = False Then
        MessageBox.Show("Debe seleccionar con un check el registro que desea Eliminar.", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
      Else
        If dgvListado.Item("cEstado", dgvListado.CurrentCell.RowIndex).Value = "C" Then
          MessageBox.Show("No se puede eliminar el registro, ya que se encuentra con estado CERRADO", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else

          If MessageBox.Show("¿Desea eliminar el registro seleccionado?", g_sModulo, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Call EliminarInventario(dgvListado.Item("iNumInv", dgvListado.CurrentCell.RowIndex).Value)
          End If

          Call ListarInventario(True)
        End If
      End If


    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click
    Try

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub
End Class